#include <gtk/gtk.h>
#include "network.h"
#include "stdio.h"
#include <unistd.h>


GtkWidget *janelaPrincipal;


void clique_boot(GtkWidget *w, gpointer p)
{
    gchar *file, *args;          
    file = gtk_file_chooser_get_filename (p);
    sprintf(args, "-hda \"%s\" -boot c -net nic,vlan=0 -net tap,vlan=0,ifname=tap0,script=/etc/qemu-/ifup", file);
    if (!execve("qemu",args,NULL))
         showDialog(NULL, "Error", "Nothing", 200, 100);
    
    gtk_main_quit();
}

void showDialog(GtkWidget *janela, gchar *title, gchar *message, int h, int w) {
     GtkWidget *dialog,
               *hbox,
               *labelDialog;   
     
    dialog = gtk_dialog_new_with_buttons(title, GTK_WINDOW(janela), GTK_DIALOG_MODAL, GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);    
    gtk_widget_set_size_request(dialog, h, w);
    labelDialog = gtk_label_new(message);
    
    hbox = gtk_hbox_new (FALSE, 5);
    gtk_container_set_border_width (GTK_CONTAINER (hbox), 10);
    gtk_box_pack_start_defaults (GTK_BOX (hbox), labelDialog);        
    gtk_box_pack_start_defaults (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox);
    gtk_widget_show_all (dialog);
    
    gtk_dialog_run (GTK_DIALOG (dialog));    
    gtk_widget_destroy (dialog);     
}

void clique_configure_network(GtkWidget *w, gpointer p) {
    FILE *interfacesFile, *dnsFile;    
    NetworkWidgetsBundle *bundle;
    gchar *interface, *ipAddress, *subMask, *ipGateway, *ipDns;
    bundle = (NetworkWidgetsBundle*) p;        
    
    interfacesFile = fopen("/home/ltia/Desktop/out.txt","w+");  

    if (gtk_toggle_button_get_active (bundle->checkDhcp)) {      
        fprintf(interfacesFile, "iface br0 inet dhcp");               
    } else {        
       dnsFile = fopen("/home/ltia/Desktop/out2.txt","w+");  
        ipAddress = gtk_entry_get_text(bundle->txtIpAddress);        
        subMask = gtk_entry_get_text(bundle->txtSubnetMask);        
        ipGateway = gtk_entry_get_text(bundle->txtGatewayAddress);        
        ipDns = gtk_entry_get_text(bundle->txtDnsAddress);        
        interface = gtk_combo_box_get_active_text(bundle->cmbInterface);        
        fprintf(interfacesFile, "iface %s inet static\n", interface);
        fprintf(interfacesFile, "address %s\n", ipAddress);
        fprintf(interfacesFile, "netmask %s\n", subMask);            
        fprintf(interfacesFile, "gateway %s\n", ipGateway);            
        fprintf(dnsFile, "nameserver %s", ipDns);
        fclose(dnsFile);          
    }   
    fclose(interfacesFile);  
    showDialog(NULL, "Success", "Configuration Done!", 200, 100);
}

void check_toggled(GtkWidget *w, gpointer p)
{
    NetworkWidgetsBundle *bundle = (NetworkWidgetsBundle *) p;
    
    if (GTK_WIDGET_SENSITIVE(GTK_WIDGET(bundle->cmbInterface))) 
    {
        gtk_widget_set_sensitive (bundle->cmbInterface, FALSE);
        gtk_widget_set_sensitive (bundle->txtIpAddress, FALSE);
        gtk_widget_set_sensitive (bundle->txtSubnetMask, FALSE);
        gtk_widget_set_sensitive (bundle->txtGatewayAddress, FALSE);
        gtk_widget_set_sensitive (bundle->txtDnsAddress, FALSE);
   }
   else 
   {
        gtk_widget_set_sensitive (bundle->cmbInterface, TRUE);
        gtk_widget_set_sensitive (bundle->txtIpAddress, TRUE);
        gtk_widget_set_sensitive (bundle->txtSubnetMask, TRUE);
        gtk_widget_set_sensitive (bundle->txtGatewayAddress, TRUE);
        gtk_widget_set_sensitive (bundle->txtDnsAddress, TRUE);
    }
}

void verifica_arquivo(gpointer p)
{
    FILE *arq, *arqdns;
    gchar frase[50], f1[20], s;
    gchar* txt;
    arq = fopen("/home/ltia/Desktop/out.txt","r");
    arqdns = fopen("/home/ltia/Desktop/out2.txt","r");
    gint col;
    if (!arq)
        return;
    int i;
    NetworkWidgetsBundle *bundle = (NetworkWidgetsBundle *) p;
     fgets(frase,50,arq);
    if (dhcp(frase))
    {
        gtk_toggle_button_set_active(bundle->checkDhcp, TRUE);
        check_toggled(NULL, (gpointer)bundle);
    }
    else
    {
//Combo
         txt = text_combo(frase);
         col = col_combo(bundle->cmbInterface, txt);
         gtk_combo_box_set_active (bundle->cmbInterface, col);
//IP Adress
        for(i=0;i<8;i++)
            fgetc(arq);
        for (i=0;i<20;i++)
            f1[i] = ' ';
        s = fgetc(arq);
        i = 0;
        while(s !='\n')
        {
            f1[i] = s;
            s = fgetc(arq);
             i++;
        }
        gtk_entry_set_text(GTK_ENTRY(bundle->txtIpAddress), f1);
//Subnet Mask
        for(i=0;i<8;i++)
            fgetc(arq);
        for (i=0;i<20;i++)
            f1[i] = ' '; 
        i = 0;
        s = fgetc(arq);
        while(s !='\n')
        {
            f1[i] = s;
            s = fgetc(arq);
            i++;
        }
        gtk_entry_append_text(GTK_ENTRY(bundle->txtSubnetMask),f1);
//Gateway
        for(i=0;i<8;i++)
            fgetc(arq);
        for (i=0;i<20;i++)
            f1[i] = ' '; 
        i = 0;
        s = fgetc(arq);
        while(s !='\n' && s != EOF) {
            f1[i] = s;
            i++;
            s = fgetc(arq);
        }
        gtk_entry_append_text(GTK_ENTRY(bundle->txtGatewayAddress), f1);
//DNS
        for(i=0;i<11;i++)
            fgetc(arqdns);
        for (i=0;i<20;i++)
            f1[i] = ' ';  
        i = 0;
        s = fgetc(arqdns);
        while(s !='\n' && s != EOF) {
            f1[i] = s;
            i++;
            s = fgetc(arqdns);
        }
         gtk_entry_append_text(GTK_ENTRY(bundle->txtDnsAddress), f1);
    }
}

void clique_network(GtkWidget *w, gpointer p)
{
    GtkWidget *janela,
              *combo, 
              *table, 
              *labelInterfaceRede,
              *labelIpAddress,
              *labelSubnetMask,
              *labelGatewayAddress,
              *labelDnsAddress,
              *checkDhcp,
              *txtIpAddress,
              *txtSubnetMask,
              *txtGatewayAddress,
              *txtDnsAddress,
              *buttonClose,
              *buttonConfigure; 
    
    NetworkWidgetsBundle *bundle;   
    
    //Janela
    janela = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(janela), "Network Configuration");
    gtk_widget_set_size_request (janela, 350, 300);
    gtk_container_set_border_width (GTK_CONTAINER (janela), 5);
    
    //Table
    table = gtk_table_new (7, 2, TRUE);    
    gtk_table_set_row_spacings (GTK_TABLE (table), 5);
    gtk_table_set_col_spacings (GTK_TABLE (table), 5);      
    
    //DCHP
    checkDhcp = gtk_check_button_new_with_label ("Automatically Configuration (DHCP)");
    gtk_table_attach(GTK_TABLE (table), checkDhcp, 0, 2, 0, 1, GTK_FILL, GTK_SHRINK, 0, 0);    
    
    //Interface de Rede 
    //Label
    labelInterfaceRede = gtk_label_new ("Network Interface:");
    gtk_table_attach(GTK_TABLE (table), labelInterfaceRede, 0, 1, 1, 2, GTK_FILL, GTK_SHRINK, 0, 0);
    //Combobox
    combo = gtk_combo_box_new_text ();
    escreve_combo(combo);
    gtk_table_attach(GTK_TABLE (table), combo, 1, 2, 1, 2, GTK_FILL, GTK_SHRINK, 0, 0); 

    //IP Address
    //Label
    labelIpAddress = gtk_label_new ("IP Address:");
    gtk_table_attach(GTK_TABLE (table), labelIpAddress, 0, 1, 2, 3, GTK_FILL, GTK_SHRINK, 0, 0);    
    //Textbox
    txtIpAddress = gtk_entry_new();
    gtk_table_attach(GTK_TABLE (table), txtIpAddress, 1, 2, 2, 3, GTK_FILL, GTK_SHRINK, 0, 0);    
    
    //Subnet Mask
    //Label
    labelSubnetMask = gtk_label_new ("Subnet Mask:");
    gtk_table_attach(GTK_TABLE (table), labelSubnetMask, 0, 1, 3, 4, GTK_FILL, GTK_SHRINK, 0, 0);    
    //Textbox
    txtSubnetMask = gtk_entry_new();
    gtk_table_attach(GTK_TABLE (table), txtSubnetMask, 1, 2, 3, 4, GTK_FILL, GTK_SHRINK, 0, 0);    
    
    //Gateway Address
    //Label
    labelGatewayAddress = gtk_label_new ("Gateway Address:");
    gtk_table_attach(GTK_TABLE (table), labelGatewayAddress, 0, 1, 4, 5, GTK_FILL, GTK_SHRINK, 0, 0);    
    //Textbox
    txtGatewayAddress = gtk_entry_new();
    gtk_table_attach(GTK_TABLE (table), txtGatewayAddress, 1, 2, 4, 5, GTK_FILL, GTK_SHRINK, 0, 0);    
    
    //DNS Address
    //Label
    labelDnsAddress = gtk_label_new ("DNS Address:");
    gtk_table_attach(GTK_TABLE (table), labelDnsAddress, 0, 1, 5, 6, GTK_FILL, GTK_SHRINK, 0, 0);    
    //Textbox
    txtDnsAddress = gtk_entry_new();
    gtk_table_attach(GTK_TABLE (table), txtDnsAddress, 1, 2, 5, 6, GTK_FILL, GTK_SHRINK, 0, 0);    
        
    //Buttons
    buttonConfigure = gtk_button_new_from_stock (GTK_STOCK_APPLY);
    gtk_table_attach(GTK_TABLE (table), buttonConfigure, 0, 1, 6, 7, GTK_FILL, GTK_SHRINK, 0, 0);     
    
    
    buttonClose = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
    gtk_table_attach(GTK_TABLE (table), buttonClose, 1, 2, 6, 7, GTK_FILL, GTK_SHRINK, 0, 0);    
    g_signal_connect_swapped (G_OBJECT (buttonClose), "clicked", G_CALLBACK (gtk_widget_destroy), (gpointer) janela);    
       
    //Signals Connect    
    bundle = (NetworkWidgetsBundle*) malloc(sizeof(NetworkWidgetsBundle));
    bundle->txtGatewayAddress = txtGatewayAddress;
    bundle->txtIpAddress = txtIpAddress;
    bundle->txtSubnetMask = txtSubnetMask;
    bundle->txtDnsAddress = txtDnsAddress;
    bundle->checkDhcp = checkDhcp;
    bundle->cmbInterface = combo;
    verifica_arquivo((gpointer)bundle);
    
    g_signal_connect (G_OBJECT (buttonConfigure), "clicked", 
                                  G_CALLBACK (clique_configure_network), (gpointer) bundle);
    g_signal_connect (G_OBJECT (checkDhcp), "toggled", 
                                  G_CALLBACK (check_toggled), (gpointer) bundle);
  
    
    gtk_container_add (GTK_CONTAINER (janela), table);    
    gtk_window_move(GTK_WINDOW (janela),300,150);
    gtk_widget_show_all(janela);
} 

void escreve_combo(GtkWidget **combo)
{
    dev lista = NULL;
    gint i=0;
    sysfs(&lista);
    while (lista) {
        gtk_combo_box_insert_text(combo, i,lista->name);
        i++;
        lista = lista->pr;
    }
 
}

void desenha_principal(){
    GtkWidget *buttonboot, 
                      *buttonnet,
                      *table,   
                      *labelname, 
                      *chooser;    

    //Janela
    janelaPrincipal = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(janelaPrincipal), "LiMiddle");
    gtk_window_move(GTK_WINDOW (janelaPrincipal),300,150);
    gtk_widget_set_size_request (janelaPrincipal, 500, 150);
    gtk_container_set_border_width (GTK_CONTAINER (janelaPrincipal), 10);
      
    //Table
    table = gtk_table_new (3, 3, TRUE);    
    gtk_table_set_row_spacings (GTK_TABLE (table),20);
    gtk_table_set_col_spacings (GTK_TABLE (table), 5);      

    //Label
    labelname = gtk_label_new ("LiMiddle - Startup Manager Application");
    gtk_table_attach(GTK_TABLE (table), labelname, 0, 3, 0, 1, GTK_FILL, GTK_SHRINK, 0, 0);    
    
    //File Chooser
   chooser = gtk_file_chooser_button_new ("Chooser a Folder", GTK_FILE_CHOOSER_ACTION_OPEN);
   gtk_table_attach(GTK_TABLE (table), chooser, 0, 3, 1, 2, GTK_FILL, GTK_SHRINK, 0, 0);

       
    //Botões
    buttonboot = gtk_button_new_with_label("Boot Selected Image");
    gtk_signal_connect(GTK_OBJECT (buttonboot), "clicked", GTK_SIGNAL_FUNC (clique_boot), chooser);
    gtk_table_attach(GTK_TABLE (table), buttonboot, 0, 1, 2, 3, GTK_FILL, GTK_SHRINK, 0, 0);
 
    buttonnet = gtk_button_new_with_label("Network Configuration");
    gtk_signal_connect(GTK_OBJECT (buttonnet), "clicked", GTK_SIGNAL_FUNC (clique_network), NULL);
    gtk_table_attach(GTK_TABLE (table), buttonnet, 2, 3, 2, 3, GTK_FILL, GTK_SHRINK, 0, 0);
    
    
    gtk_container_add (GTK_CONTAINER (janelaPrincipal), table);
    gtk_widget_show_all(janelaPrincipal);
}


int main (int argc, char **argv) {
    gtk_init(&argc, &argv);
    desenha_principal();
    gtk_main();
    return 0; 
}
